import React from 'react';
import { connect } from 'react-redux';
import BaseComponent from '../../components/common/baseComponent.js';
import Loading from '../../components/common/loading';
import { get, save } from '../../utils/storage';
import { getQueryParam } from '../../utils/url';

@connect(() => {
  return {};
})
class LoginWithCode extends BaseComponent {
  componentDidMount() {
    const preCode = get('yyeCode');
    const yyeCode = getQueryParam('yyeCode');
    const { redirect } = this.props.location.state || { redirect: '/cq/home' };
    console.log('=========', redirect, preCode, yyeCode);
    if (yyeCode && `${yyeCode}` !== `${preCode}`) {
      save('yyeCode', yyeCode);
      this.props.dispatch({
        type: 'user/loginWithCode',
        payload: {
          yyeCode,
          fromTmall: window.source === 'TAOBAO' || redirect.indexOf('/tmall') !== -1,
          redirect,
        },
      });
    } else {
      console.log('重复的yyecode');
      this.props.history.replace(redirect);
    }
  }
  render() {
    return <Loading />;
  }
}

export default LoginWithCode;
